local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "agenthelper"
local msgproxy = require "msgproxy"
local processstate = require "processstate"
local table = table
require "enum"

local processing = processstate:new({timeout=4})
local  GetCurrGameBoardRecord = {}

--[[
//请求当前游戏局的战绩
message GetCurrGameBoardRecordReq {
	optional Version version = 1;
	optional gameid = 2;	
}

//响应当前游戏局的战绩
message GetCurrGameBoardRecordRes {
	optional int32 errcode = 1;
	optional string errcodedes = 2;
	optional int32 gameid = 3;	
}

]]

function  GetCurrGameBoardRecord.process(session, source, fd, request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
		gameid = request.gameid,
	}
	local server = msghelper:get_server()

	--检查当前登陆状态
	if not msghelper:is_login_success() then
		filelog.sys_warning("GetCurrGameBoardRecord.process invalid server state", server.state)
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求！"
		msghelper:send_resmsgto_client(fd, "GetCurrGameBoardRecordRes", responsemsg)		
		return
	end

	if processing:is_processing() then
		responsemsg.errcode = EErrCode.ERR_DEADING_LASTREQ
		responsemsg.errcodedes = "正在处理上一次请求！"
		msghelper:send_resmsgto_client(fd, "GetCurrGameBoardRecordRes", responsemsg)		
		return
	end

	if server.gameid ~= request.gameid then
		responsemsg.errcode = EErrCode.ERR_INVALID_PARAMS
		responsemsg.errcodedes = "无效的请求参数！"
		msghelper:send_resmsgto_client(fd, "GetCurrGameBoardRecordRes", responsemsg)		
		return		
	end

	if server.roomsvr_id == "" then
		responsemsg.errcode = EErrCode.ERR_INVALID_PARAMS
		responsemsg.errcodedes = "无效的请求参数！"
		msghelper:send_resmsgto_client(fd, "GetCurrGameBoardRecordRes", responsemsg)		
		return		
	end

	if server.roomsvr_table_id <= 0 then
		responsemsg.errcode = EErrCode.ERR_INVALID_PARAMS
		responsemsg.errcodedes = "无效的请求参数！"
		msghelper:send_resmsgto_client(fd, "GetCurrGameBoardRecordRes", responsemsg)		
		return		
	end

	request.rid = server.rid
	request.gatesvr_id = skynet.getenv("svr_id")
	request.agent_address = skynet.self()
	processing:set_process_state(true)
	responsemsg = msgproxy.sendrpc_reqmsgto_roomsvrd(request.gameid, nil, server.roomsvr_id, server.roomsvr_table_address, "getcurrgameboardrecord", request)
	processing:set_process_state(false)

	if not msghelper:is_login_success() then
		return
	end

	if responsemsg == nil then
		responsemsg = {
			errcode = EErrCode.ERR_INVALID_REQUEST,
			errcodedes = "无效的请求!",
			gameid = request.gameid,
		}
	end

	msghelper:send_resmsgto_client(fd, "GetCurrGameBoardRecordRes", responsemsg)
end

return GetCurrGameBoardRecord

